package com.ruoyi.web.service;

import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.vo.BigScreenVo;
import com.ruoyi.web.domain.vo.ZTScreenVo;
import com.ruoyi.web.mapper.BigScreenMapper;
import com.ruoyi.web.vo.MachineTask;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BigScreenService {
    @Resource
    private BigScreenMapper bigScreenMapper;

    /**
     * 左上角查询 - 查询各机台实际用纸令数（一、四车间）
     * @Date: 2024年7月22日
     * @Auth: 张恒
     */
    public List<BigScreenVo> getMachineLing(BigScreenVo bigScreenVo){
        if(StringUtils.isEmpty(bigScreenVo.getWorkshopId())){
            throw new BaseException("未获取到车间信息");
        }
        // 获取当前时间
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            bigScreenVo.setBeginDate(dateFormat.parse(year + "-" + month + "-01 00:00:00"));
            bigScreenVo.setEndDate(dateFormat.parse(year + "-" + (month + 1) + "-01 00:00:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return bigScreenMapper.selectMachineLing(bigScreenVo);
    }

    /**
     * 中间查询 - 查询当日实际用纸总令数（一、四车间）
     * @Date: 2024年7月22日
     * @Auth: 张恒
     */
    public BigDecimal getSumLing(BigScreenVo bigScreenVo){
        if(StringUtils.isEmpty(bigScreenVo.getWorkshopId())){
            throw new BaseException("未获取到车间信息");
        }
        // 获取当前时间
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            bigScreenVo.setBeginDate(dateFormat.parse(year + "-" + month + "-" + day + " 00:00:00"));
            bigScreenVo.setEndDate(dateFormat.parse(year + "-" + month + "-" + day  + " 23:59:59"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BigDecimal decimal = bigScreenMapper.selectSumLing(bigScreenVo);
        if(decimal != null){
            decimal = new BigDecimal(decimal.stripTrailingZeros().toPlainString());
        }else {
            decimal = new BigDecimal(0);
        }
        return decimal;
    }

    /**
     * 左上角查询 - 查询各机台装订报工数量（二、三车间）
     * @Date: 2024年7月23日
     * @Auth: 张恒
     */
    public List<BigScreenVo> getMachineBinding(BigScreenVo bigScreenVo){
        if(StringUtils.isEmpty(bigScreenVo.getWorkshopId())){
            throw new BaseException("未获取到车间信息");
        }
        // 获取当前时间
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            bigScreenVo.setBeginDate(dateFormat.parse(year + "-" + month + "-01 00:00:00"));
            bigScreenVo.setEndDate(dateFormat.parse(year + "-" + (month + 1) + "-01 00:00:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 根据车间以及日期查询主表信息，并根据机台分组
        List<BigScreenVo> bigScreenVos = bigScreenMapper.selectGroupByMachine(bigScreenVo);
        ArrayList<BigScreenVo> bigScreenVoList = new ArrayList<>();
        for (BigScreenVo screenVo : bigScreenVos) {
            String machineName = screenVo.getMachineName(); // 机台ID
            // 查询各机台下对应的所有报工单明细
            String bindingDailyId = screenVo.getBindingDailyId();
            String[] split = bindingDailyId.split(",");
            List<BigScreenVo> screenVos = bigScreenMapper.selectBindingDailyDetail(split);
            BigDecimal completeCount = new BigDecimal(0);
            for (BigScreenVo vo : screenVos) {
                BigDecimal decimal = vo.getCompleteCount() == null ? new BigDecimal(0) : vo.getCompleteCount();
                completeCount = completeCount.add(decimal);
            }
            BigScreenVo screenVo1 = new BigScreenVo();
            screenVo1.setMachineName(machineName);
            screenVo1.setCompleteCount(completeCount);
            bigScreenVoList.add(screenVo1);
        }
        // 排序
        for (int i = 0; i < bigScreenVoList.size() - 1; i++) {
            for (int j = 0; j < bigScreenVoList.size() - 1 - i; j++) {
                if(bigScreenVoList.get(j).getCompleteCount().compareTo(bigScreenVoList.get(j + 1).getCompleteCount()) > 0){
                    BigScreenVo vo1 = bigScreenVoList.get(j);
                    bigScreenVoList.set(j, bigScreenVoList.get(j + 1));
                    bigScreenVoList.set(j + 1, vo1);
                }
            }
        }
        // 倒序
        Collections.reverse(bigScreenVoList);
        return bigScreenVoList;
    }

    /**
     * 中间查询 - 查询当日报工汇总册数（二、三车间）
     * @Date: 2024年7月23日
     * @Auth: 张恒
     */
    public BigDecimal getSumBinding(BigScreenVo bigScreenVo){
        if(StringUtils.isEmpty(bigScreenVo.getWorkshopId())){
            throw new BaseException("未获取到车间信息");
        }
        // 获取当前时间
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            bigScreenVo.setBeginDate(dateFormat.parse(year + "-" + month + "-" + day + " 00:00:00"));
            bigScreenVo.setEndDate(dateFormat.parse(year + "-" + month + "-" + day  + " 23:59:59"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 根据车间以及日期查询主表信息，并根据机台分组
        List<BigScreenVo> bigScreenVos = bigScreenMapper.selectMachine(bigScreenVo);
        BigDecimal completeCount = new BigDecimal(0);
        for (BigScreenVo screenVo : bigScreenVos) {
            BigDecimal decimal = screenVo.getCompleteCount() == null ? new BigDecimal(0) : screenVo.getCompleteCount();
            completeCount = completeCount.add(decimal);
        }
        return new BigDecimal(completeCount.stripTrailingZeros().toPlainString());
    }

    /**
     * 中间查询 - 查询生产单信息（全部车间）
     * @Date: 2024年7月22日
     * @Auth: 张恒
     */
    public BigScreenVo getSumProduce(BigScreenVo bigScreenVo){
        // 获取当前时间
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            bigScreenVo.setBeginDate(dateFormat.parse(year + "-" + month + "-" + day + " 00:00:00"));
            bigScreenVo.setEndDate(dateFormat.parse(year + "-" + month + "-" + day  + " 23:59:59"));
            bigScreenVo.setBeginDate2(dateFormat.parse(year + "-" + month + "-01 00:00:00"));
            bigScreenVo.setEndDate2(dateFormat.parse(year + "-" + (month + 1) + "-01 00:00:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BigScreenVo screenVo = bigScreenMapper.selectSumProduce(bigScreenVo);
        return screenVo;
    }

    /**
     * 右上角查询 - 查询生产单信息（全部车间）
     * @Date: 2024年7月23日
     * @Auth: 张恒
     */
    public List<BigScreenVo> getProduce(BigScreenVo bigScreenVo){
        List<BigScreenVo> screenVo = bigScreenMapper.selectProduce(bigScreenVo);
        return screenVo;
    }


    /**
     * 数据中台 - 设备类型数量
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<ZTScreenVo> getMachineCount_ZT(ZTScreenVo ztScreenVo){
        List<ZTScreenVo> screenVo = bigScreenMapper.selectMachineCount_ZT(ztScreenVo);
        return screenVo;
    }

    /**
     * 数据中台 - 设备名称,环节,运行状态
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<ZTScreenVo> getMachineInfo_ZT(ZTScreenVo ztScreenVo){
        List<ZTScreenVo> screenVo = bigScreenMapper.selectMachineInfo_ZT(ztScreenVo);
        return screenVo;
    }

    /**
     * 数据中台 - 产线运行数量
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<ZTScreenVo> getLineCount_ZT(ZTScreenVo ztScreenVo){
        List<ZTScreenVo> screenVo = bigScreenMapper.selectLineCount_ZT(ztScreenVo);
        return screenVo;
    }

    /**
     * 数据中台 - 原料仓库
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<ZTScreenVo> getMaterial_ZT(ZTScreenVo ztScreenVo){
        List<ZTScreenVo> screenVo = bigScreenMapper.selectMaterial_ZT(ztScreenVo);
        return screenVo;
    }

    /**
     * 数据中台 - 生产计划
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<ZTScreenVo> getProducePlan_ZT(ZTScreenVo ztScreenVo){
        List<ZTScreenVo> screenVo = bigScreenMapper.selectProducePlan_ZT(ztScreenVo);
        return screenVo;
    }

    /**
     * 数据中台 - 产量情况
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<ZTScreenVo> getProduceYield_ZT(ZTScreenVo ztScreenVo){
        List<ZTScreenVo> screenVo = bigScreenMapper.selectProduceYield_ZT(ztScreenVo);
        return screenVo;
    }

    /**
     * 数据中台 - 生产明细
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<MachineTask> getProduceInfo_ZT(MachineTask machineTask){
        List<MachineTask> listMachineTask = bigScreenMapper.selectProduceInfo_ZT(machineTask);
        return listMachineTask;
    }

    /**
     * 数据中台 - 本年度产量走势分析
     * @Date: 2024年7月24日
     * @Auth: levicash
     */
    public List<ZTScreenVo> getYearTrend_ZT(ZTScreenVo ztScreenVo){
        List<ZTScreenVo> screenVo = bigScreenMapper.selectYearTrend_ZT(ztScreenVo);
        return screenVo;
    }
}
